<?php
/**
 * Created by PhpStorm.
 * User: cqfut
 * Date: 2019/04/01
 */
require_once (ROOTDIR."common/db_utils.php");
class HomeworkModel {

    public function __construct() {

    }

    /**
     * 获取作业列表
     * @param $hwstatus 作业状态
     * @param $problemId 题目ID
     * @param $stuId 学生ID
     * @param int $beginIdx 分页起始下标
     * @param int $cnt 每页数量
     * @return array
     */
    public function getHomeworkList($hwstatus, $problemId, $stuId, $beginIdx = 0, $cnt = 20 ) {

        $sqlWhere = ""; // where 子句
        $args=array();

        // 仅限查询指定用户的暂存代码
        $sqlWhere = "  where stu_id=? ";
        array_push($args, $stuId);

        // 添加problemId过滤
        if ( $problemId > 0 ) {
            $sqlWhere .= "  and problem_id=? ";
            array_push($args, $problemId);
        }

        // 添加作业状态过滤
        if ( $hwstatus > 0 ) {
            $sqlWhere .= "  and hw_status=? ";
            array_push($args, $hwstatus);
        }

        $colNames = array('hw_id', 'hw_name', 'problem_id','problem_name',
            'stu_id', 'teacher_id', 'create_time', 'hw_status',
            'solution_id', 'submit_time', 'is_remarked');
        $sql = DbUtil::genSelectSql($colNames, "homework");

        $sql.=$sqlWhere;
        $sql.="  order by `create_time` desc limit ".$beginIdx.", ".$cnt;

        // 特别注意：limit后面的数字不能作为$args
        // 执行数据库查询语句
        $result = DbUtil::getInstance()->query2($sql, $args);
        $allHomeworkList= array();
        if ( count($result) == 0 ) {
            return $allHomeworkList;
        }
        $oneHomework =array();
        foreach ($result as $row ) {
            $oneHomework['id'] = urlencode($row['hw_id']);
            $oneHomework['name'] = urlencode($row['hw_name']);
            $oneHomework['pid'] = intval($row['problem_id']);
            $oneHomework['pname'] = urlencode($row['problem_name']);
            $oneHomework['isremarked'] = intval($row['is_remarked']);
            $oneHomework['hwstatus'] = intval($row['hw_status']);
            $oneHomework['sol_id'] = intval($row['solution_id']);
            $oneHomework['create_time'] = substr($row['create_time'],0,-3);
            $oneHomework['submit_time'] = substr($row['submit_time'], 0, -3);
            array_push($allHomeworkList, $oneHomework);
        }
        return $allHomeworkList;
    }

    /**
     * 获取课后练习数量
     * @param $statusType 完成状态(-1 表示不区分完成状态)
     * @param $problemId 题目ID(-1 表示不区分题目ID)
     * @param $stuId 学生ID
     */
    public function getHomeworkCnt($hwstatus, $problemId, $stuId) {

        $sqlWhere = ""; // where 子句
        $args=array();

        // 仅限查询指定用户的暂存代码
        $sqlWhere = "  where stu_id=? ";
        array_push($args, $stuId);

        // 添加problemId过滤
        if ( $problemId > 0 ) {
            $sqlWhere .= "  and problem_id=? ";
            array_push($args, $problemId);
        }

        // 添加作业状态过滤
        if ( $hwstatus > 0 ) {
            $sqlWhere .= "  and hw_status=? ";
            array_push($args, $hwstatus);
        }

        $sql= "select count(`hw_id`) from homework ".$sqlWhere;
        $result = DbUtil::getInstance()->query2($sql, $args);
        if ( count($result) == 0 ) {
            return 0;
        }
        return intval($result[0][0]);
    }
}